package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

/**
 * 添加/编辑禁用话题Activity
 */
public class AddBlockedTopicActivity extends AppCompatActivity {

    public static final String EXTRA_TOPIC_ID = "topic_id";

    private TopicRepository repository;
    private BlockedTopic blockedTopic;
    private boolean isEditMode = false;

    // UI组件
    private EditText editTopic;
    private ImageView btnClose;
    private ImageView btnConfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_add_blocked_topic);

        repository = new TopicRepository(this);
        
        // 检查是否是编辑模式
        String topicId = getIntent().getStringExtra(EXTRA_TOPIC_ID);
        if (topicId != null) {
            isEditMode = true;
            blockedTopic = repository.getTopicById(topicId);
            if (blockedTopic == null) {
                Toast.makeText(this, "未找到话题", Toast.LENGTH_SHORT).show();
                finish();
                return;
            }
        } else {
            blockedTopic = new BlockedTopic();
        }

        initViews();
        updateUI();
        setupListeners();
    }

    private void initViews() {
        editTopic = findViewById(R.id.edit_topic);
        btnClose = findViewById(R.id.btn_close);
        btnConfirm = findViewById(R.id.btn_confirm);
    }

    private void updateUI() {
        if (isEditMode) {
            editTopic.setText(blockedTopic.getTopic());
            TextView title = findViewById(R.id.text_title);
            if (title != null) {
                title.setText("编辑话题");
            }
        }
    }

    private void setupListeners() {
        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveTopic();
            }
        });
    }

    private void saveTopic() {
        String topicText = editTopic.getText().toString().trim();
        
        if (topicText.isEmpty()) {
            Toast.makeText(this, "请输入话题内容", Toast.LENGTH_SHORT).show();
            return;
        }

        // 检查是否重复（编辑模式下排除当前话题）
        if (!isEditMode || !topicText.equals(blockedTopic.getTopic())) {
            // 检查是否已存在相同话题
            java.util.List<BlockedTopic> allTopics = repository.getAllTopics();
            for (BlockedTopic topic : allTopics) {
                if (topic.getTopic().equals(topicText)) {
                    Toast.makeText(this, "该话题已存在", Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        }

        blockedTopic.setTopic(topicText);

        if (isEditMode) {
            repository.updateTopic(blockedTopic);
            Toast.makeText(this, "已更新", Toast.LENGTH_SHORT).show();
        } else {
            repository.addTopic(blockedTopic);
            Toast.makeText(this, "已添加", Toast.LENGTH_SHORT).show();
        }

        finish();
    }
}

